package com.xiaomi.shwx.controller;

import com.xiaomi.shwx.common.result.Result;
import com.xiaomi.shwx.entity.dto.WorkOrderDTO;
import com.xiaomi.shwx.entity.pojo.WorkOrder;
import com.xiaomi.shwx.entity.request.CancelWorkOrderReq;
import com.xiaomi.shwx.entity.request.WorkOrderSearchRequest;
import com.xiaomi.shwx.entity.vo.WorkOrderListVO;
import com.xiaomi.shwx.entity.vo.WorkOrderVO;
import com.xiaomi.shwx.service.WorkOrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: after-sales-maintenance
 * @description: 工单controller
 * @author: Yangziyin
 * @create: 2024-05-19 11:15
 **/
@RestController
@RequestMapping("/api/workorder")
public class WorkOrderController {
    @Resource
    private WorkOrderService workOrderService;

    @PostMapping("/list")
    public Result<WorkOrderListVO> getOrderList(@RequestBody WorkOrderSearchRequest request) {
        List<WorkOrderVO> workOrderList = workOrderService.listOrder(request);
        WorkOrderListVO workOrderListVO = new WorkOrderListVO();
        workOrderListVO.setWorkOrderList(workOrderList);
        workOrderListVO.setTotal(workOrderList.size());
        return Result.ok(workOrderListVO);
    }

    @GetMapping("/list/initial")
    public Result<WorkOrderListVO> getInitialOrderList() {
        List<WorkOrderVO> workOrderList = workOrderService.listInitialOrder();
        WorkOrderListVO res = new WorkOrderListVO();
        res.setWorkOrderList(workOrderList);
        res.setTotal(workOrderList.size());
        return Result.ok(res);
    }

    @PostMapping("/add")
    public Result<WorkOrderVO> addOrder(@RequestBody WorkOrder order) {
        return Result.ok(workOrderService.addOrder(order));
    }

    /**
     * 用户创建维修工单
     *
     * @param dto
     * @return
     */
    @PostMapping("/create")
    public Result<String> createOrder(@RequestBody WorkOrderDTO dto) {
        return Result.ok(workOrderService.createOrder(dto));
    }

    /**
     * 用户取消工单
     *
     * @param cancelWorkOrderReq
     * @return
     */
    @PostMapping("/cancel")
    public Result<String> cancelWorkOrder(@RequestBody CancelWorkOrderReq cancelWorkOrderReq) {
        return Result.ok(workOrderService.cancelOrder(cancelWorkOrderReq.getId()));
    }

    /**
     * 用户查看个人工单列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/orders/{userId}")
    public Result<List<WorkOrderVO>> getWorkOrders(@PathVariable String userId) {
        return Result.ok(workOrderService.getOrdersByUserId(userId));
    }

    /**
     * 用户查看个人工单详细信息
     *
     * @param workorderId
     * @return
     */
    @GetMapping("/details/{workorderId}")
    public Result<WorkOrderVO> getWorkOrder(@PathVariable String workorderId) {
        return Result.ok(workOrderService.userGetOrderById(workorderId));
    }

    /**
     * 用户/工程师更新工单信息
     *
     * @param id
     * @return
     */
    @PostMapping("/update")
    public Result<WorkOrderVO> updateWorkOrder(@PathVariable(value = "id") String id) {
        WorkOrder order = new WorkOrder();
        order.setId(Long.valueOf(id));
        WorkOrderVO res = workOrderService.updateOrder(order);
        return Result.ok(res);
    }

    /**
     * 支付工单
     *
     * @param id
     * @return
     */
    @GetMapping("/pay/{id}")
    public Result<WorkOrderVO> payOrder(@PathVariable(value = "id") Long id) {
        WorkOrder workOrder = new WorkOrder();
        workOrder.setId(id);
        return Result.ok(workOrderService.payOrder(workOrder));
    }

    /**
     * 确认工单
     *
     * @param order
     * @return
     */
    @PostMapping("/confirm")
    public Result<WorkOrderVO> confirmOrder(@RequestBody WorkOrder order) {
        return Result.ok(workOrderService.confirmOrder(order));
    }

    @GetMapping("/recheck/done/{id}")
    public Result<WorkOrderVO> recheckDone(@PathVariable(value = "id") Long id) {
        WorkOrder workOrder = new WorkOrder();
        workOrder.setId(Long.valueOf(id));
        return Result.ok(workOrderService.recheckDone(workOrder));
    }

}
